<?php

namespace app\admin\service;

use app\admin\model\BoxYunOrder;
use app\qsapi\model\yunPhone\YunPhoneOrder;

/**
 * 云挂机服务类
 */
class BoxYunOrderService extends BaseService
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new YunPhoneOrder();
    }


    public function getList(): array
    {
        $params = request()->param();
        $page  = request()->param('page', 1);
        $limit = request()->param('limit', 10);
        $where  = [];
        if (!empty($params['member_id'])) {
            $where[] = ['c.id', '=', $params['member_id']];
        }
        if (!empty($params['good_name'])) {
            $where[] = ['b.name', 'like', '%' . $params['name'] . '%'];
        }
        if (!empty($params['nickname'])) {
            $where[] = ['c.nickname', 'like', '%' . $params['nickname'] . '%'];
        }
        if (!empty($params['order_id'])) {
            $where[] = ['a.order_num', '=', $params['order_id']];
        }
        if (!empty($params['order_type'])) {
            $where[] = ['a.yun_type', '=', $params['order_type']];
        }
        if (!empty($params['order_status'])) {
            $where[] = ['a.status', '=', $params['order_status']];
        }
        $pay_status = $params['pay_status'] ?? "";
        if ($pay_status != '') {
            $where[] = ['a.pay_status', '=', $params['pay_status']];
        }
        if (!empty($params['time_range'][0]) && !empty($params['time_range'][1])) {
            $where[] = ['a.create_time', 'between', [$params['time_range'][0], $params['time_range'][1]]];
        }

        $list = $this->model->alias('a')
            ->join('cq_member c', 'a.member_id = c.id')
            ->field('a.id,a.member_id,a.status order_status,a.order_num,a.order_type,a.create_time,c.nickname,a.num')
            ->order('a.id', 'desc')
            ->where('a.mark', '=', 1)
            ->where($where)
            ->paginate($limit)->toArray();

        return array (
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $list['data'],
            "count" => $list['total'],
        );
    }

    /**
     * 添加或编辑
     * @return mixed
     * @since 2021/6/9
     * @author ZhangXuanNeng
     */
    public function edit()
    {
        // 参数
        $data = request()->param();
        if (!empty($data['id'])) {
            $add_stock               = $data['add_stock'] ?? 0;
            $result                  = $this->model->where('id', '=', $data['id'])->field('stock,remaining_stock')->find();
            $data['stock']           = $result['stock'] + $add_stock;
            $data['remaining_stock'] = $result['remaining_stock'] + $add_stock;
        } else {
            $data['remaining_stock'] = $data['stock'] ?? 0;
        }

        if ($data['price'] > $data['original_price']) {
            return message('售价不能大于原价', 0);
        }

        return parent::edit($data); // TODO: Change the autogenerated stub
    }

    public function delete()
    {
        return parent::delete(); // TODO: Change the autogenerated stub
    }
}